﻿
/// <reference path="_namespace.js" />
/// <reference path="../BaseObject.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.io.Keyboard
/*****************************************************************************/

ajax.lang.addNamespace("ajax.io");
$imports("ajax.BaseObject");

//-----------------------------------------------------------------------------
ajax.io.Keyboard = function()
{
    ajax.io.Keyboard.superConstructor.call(this);
    this.keys = ajax.io.Keyboard.getKeys(); // saves clients having to keep get the same data 
    this.keyStates      = new ajax.BaseObject();
    
    //-----------------------------------------------------------------------------
    // Event handlers
    var self = this;
    
    this.keyboardListener = function(e)
    {        
        self.keyStates[self.getKeyCode(e)] = (e.type == "keydown") ? true : false;
    }
}
//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.io.Keyboard", "ajax.BaseObject"); // Inherit
//-----------------------------------------------------------------------------


// Methods to act like DirectInput.
//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.setListenForKeys = function(win, ele, val)
{
///<summary>
/// Starts this keyboard object listening for onkeyup and onkeydown events on the passed domElement. 
/// Use in conjuction with getCurrentKeyboardState. This acts like DirectInput.
///</summary>
///<param name="win" type="window">The window delivering the events</param>
///<param name="ele" type="domElement" domElement="true">the dom element to listen to</param>
///<param name="val" type="boolean">true to start listening, false to stop listening</param>  
  
    if (val)
    {
        ajax.lang.getEvent(win, ele, "onkeydown").addListener(this.keyboardListener);
        ajax.lang.getEvent(win, ele, "onkeyup").addListener(this.keyboardListener);
    }
    else
    {
        ajax.lang.getEvent(win, ele, "onkeydown").removeListener(this.keyboardListener);
        ajax.lang.getEvent(win, ele, "onkeyup").removeListener(this.keyboardListener);
    }
}
//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.getCurrentKeyboardState = function()
{///<summary>Gets the current state of every key on the keyboard  as an array with the keycode as the index.</summary>
 ///<returns type="Array" elementType="number" elementInteger="true"/>   
    var ret = {};
    ajax.BaseObject.cast(this.keyStates).copyProperties(ret, this.keyStates, false);
    return ret;    
}
//-----------------------------------------------------------------------------






//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.getKeys = function()
{///<returns type="ajax.io.Keyboard.Keys"/>
    return this.keys;
}

//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.getKeyCode = function(e)
{///<returns type="Number" integer="true"/>
    return ajax.io.Keyboard.getKeyCode(e);
}

//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.getKeyName = function(e)
{///<returns type="string"/>
    return ajax.io.Keyboard.getKeyName(e);
}

//-----------------------------------------------------------------------------
ajax.io.Keyboard.prototype.toString = function()
{
    return "ajax.io.Keyboard";
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
ajax.io.Keyboard.cast = function(obj)
{///<returns type="ajax.io.Keyboard"/>
    return obj;    
}


//-----------------------------------------------------------------------------
// Statics
ajax.io.Keyboard.getKeyCode = function(e)
{///<returns type="number" integer="true"/>
    var intKey = e.keyCode || e.which;
    return intKey;    
}

ajax.io.Keyboard.getKeyName = function(e)
{///<returns type="string"/>
    var iKey = ajax.io.Keyboard.getKeyCode(e);
    var keys = ajax.io.Keyboard.getKeys();
    for (var prop in keys)
    {
        if (keys[prop] == iKey)
            return prop;
    }
}

ajax.io.Keyboard.getKeys = function()
{///<returns type="ajax.io.Keyboard.Keys"/>
    return new ajax.io.Keyboard.Keys();
}

ajax.io.Keyboard.Keys = function(){}
ajax.io.Keyboard.Keys.prototype = 
{
    Key0: 0x30,
    Key1: 0x31,
    Key2: 0x32,
    Key3: 0x33,
    Key4: 0x34,
    Key5: 0x35,
    Key6: 0x36,
    Key7: 0x37,
    Key8: 0x38,
    Key9: 0x39,
    KeyA: 0x41,
    KeyAdd: 0x6B,
    KeyB: 0x42,
    KeyBack: 8,
    KeyC: 0x43,
    KeyCancel: 3,
    KeyCapital: 0x14,
    KeyClear: 12,
    KeyControl: 0x11,
    KeyD: 0x44,
    KeyDecimal: 0x6E,
    KeyDelete: 0x2E,
    KeyDivide: 0x6F,
    KeyDown: 0x28,
    KeyE: 0x45,
    KeyEnd: 0x23,
    KeyEscape: 0x1B,
    KeyExecute: 0x2B,
    KeyF: 0x46,
    KeyF1: 0x70,
    KeyF10: 0x79,
    KeyF11: 0x7A,
    KeyF12: 0x7B,
    KeyF13: 0x7C,
    KeyF14: 0x7D,
    KeyF15: 0x7E,
    KeyF16: 0x7F,
    KeyF2: 0x71,
    KeyF3: 0x72,
    KeyF4: 0x73,
    KeyF5: 0x74,
    KeyF6: 0x75,
    KeyF7: 0x76,
    KeyF8: 0x77,
    KeyF9: 0x78,
    KeyG: 0x47,
    KeyH: 0x48,
    KeyHelp: 0x2F,
    KeyHome: 0x24,
    KeyI: 0x49,
    KeyInsert: 0x2D,
    KeyJ: 0x4A,
    KeyK: 0x4B,
    KeyL: 0x4C,
    KeyLButton: 1,
    KeyLeft: 0x25,
    KeyM: 0x4D,
    KeyMButton: 4,
    KeyMenu: 0x12,
    KeyMultiply: 0x6A,
    KeyN: 0x4E,
    KeyNumlock: 0x90,
    KeyNumpad0: 0x60,
    KeyNumpad1: 0x61,
    KeyNumpad2: 0x62,
    KeyNumpad3: 0x63,
    KeyNumpad4: 0x64,
    KeyNumpad5: 0x65,
    KeyNumpad6: 0x66,
    KeyNumpad7: 0x67,
    KeyNumpad8: 0x68,
    KeyNumpad9: 0x69,
    KeyO: 0x4F,
    KeyP: 0x50,
    KeyPageDown: 0x22,
    KeyPageUp: 0x21,
    KeyPause: 0x13,
    KeyPrint: 0x2A,
    KeyQ: 0x51,
    KeyR: 0x52,
    KeyRButton: 2,
    KeyReturn: 13,
    KeyRight: 0x27,
    KeyS: 0x53,
    KeyScrollLock: 0x91,
    KeySelect: 0x29,
    KeySeparator: 0x6C,
    KeyShift: 0x10,
    KeySnapshot: 0x2C,
    KeySpace: 0x20,
    KeySubtract: 0x6D,
    KeyT: 0x54,
    KeyTab: 9,
    KeyU: 0x55,
    KeyUp: 0x26,
    KeyV: 0x56,
    KeyW: 0x57,
    KeyX: 0x58,
    KeyY: 0x59,
    KeyZ: 0x5A
}

ajax.io.Keyboard.Keys.cast = function(obj)
{///<returns type="ajax.io.Keyboard.Keys"/>
    return obj;
}



